<?php

namespace app\common\library;

class Result extends Code
{
    protected string $msg;

    /**
     * @param int $code 错误码
     * @param string $msg 错误消息
     * @param array|string $data 数据
     * @return array
     */
    private function result(int $code, string $msg, array|string $data): array
    {
        return array(
            'code' => $code,
            'message' => lang($msg),
            'data' => $data
        );
    }

    /**
     * 设置提示
     * @param string $msg
     * @return Result
     */
    public function setMsg(string $msg): Result
    {
        $this->msg = $msg;
        return $this;
    }

    /**
     * 成功
     * @param array $data
     * @param array|object $msg
     * @return array
     */
    public function success($data = array(), array|object $msg = self::SUCCESS): array
    {
        list($code, $message) = $msg;
        return $this->result($code, $this->msg ?? $message, $data);
    }

    /**
     * 失败
     * @param array|object $msg
     * @param array $data
     * @return array
     */
    public function fail(array|object $msg = self::FAIL, $data = array()): array
    {
        list($code, $message) = $msg;
        return $this->result($code, $this->msg ?? $message, $data);
    }
}